//////////////////////////////////////////////////////////////////////////

#ifndef _PATIENTDIALOG_H_
#include "patientdialog.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include "ui_patientdialog.h"

#ifndef _MAINWINDOW_H_
#include "mainwindow.h"
#endif

#include <QMessageBox>
#include <string>

//////////////////////////////////////////////////////////////////////////


PatientDialog::PatientDialog( QWidget* _parent )
: Dialog(_parent),
  m_ui(new Ui::PatientDialog)
{
    m_ui->setupUi(this);
}


//////////////////////////////////////////////////////////////////////////


PatientDialog::~PatientDialog()
{
    delete m_ui;
}


//////////////////////////////////////////////////////////////////////////


void PatientDialog::accept()
{
    // Collect data from window
    bool isOk;
    size_t historyNumber = m_ui->m_HistoryLineEdit->text().toUInt(&isOk);
    QString surname = m_ui->m_SurnameLineEdit->text();
    QString name = m_ui->m_NameLineEdit->text();
    QString secondname = m_ui->m_SecondnameLineEdit->text();
    QString birthDate = m_ui->m_BirthDateEdit->text();
    Human::Sex sex = m_ui->m_MaleRadioButton->isChecked() ?
                     Human::MALE : Human::FEMALE;
    QString diagnosis = m_ui->m_DiagnosisTextEdit->document()->toPlainText();
    QString notes = m_ui->m_NotesTextEdit->document()->toPlainText();

    // Check data
    if ( !isOk              ||
         surname.isEmpty()    ||
         name.isEmpty()       ||
         secondname.isEmpty() ||
         birthDate.isEmpty()  ||
         diagnosis.isEmpty() )
    {
        // Something wrong...
        QMessageBox box(QMessageBox::Warning, tr("Error"),
            tr("Please, fill all fields."),
            QMessageBox::Ok, this);

        box.exec();
        return;
    }

    // Everything is OK
    Patient patient(historyNumber,
                    surname,
                    name,
                    secondname,
                    birthDate,
                    sex,
                    diagnosis,
                    notes);

    // Update current patient
    GetMainWindow()->SetCurrPatient(patient);
}


//////////////////////////////////////////////////////////////////////////
